﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ExamAuth.aspx.cs" Inherits="Web.ExamAuth" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
	<title></title>
	<meta content="ie=7" http-equiv="x-ua-compatible">
	<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
    <META HTTP-EQUIV="Expires" CONTENT="0">
	<link href="Styles/Style01.css" rel="stylesheet" type="text/css" />
    <script src="Scripts/jquery-1.7.min.js" type="text/javascript"></script>
	<style>
		body{font-size:12px;background-color:#f1f1f1;}
		table DIV { BORDER: #88b6d2 1px solid;height:200px;text-align:center;overflow:auto;}
		tr{line-height:18px;}
		table.list tr:hover{ 
			background-color:#88b6d2!important;   /* 动态变色,IE6下无效！c4e4ff*/
		}
		.tdDel,.lblDel{cursor:hand;color:#ff4f17;}
		.tdDel:hover,.lblDel:hover{color:#fff;background:#ff4f17;}
		.choosedList td{border-bottom:1px solid #88b6d2;}
		caption{text-align:center;background:url(Images/headerfade.gif);color:#fff;font-weight:bolder;cursor:auto;}
		select{width:120px;}
	</style>
</HEAD>
<body>
<form id="Form1" runat="server">
	<table style="border: #88b6d2 1px solid;background-color:#CFDFE9;"><caption>按人员授权</caption>
		<tr>
			<td colspan="3">
				部门：<select id="Dept" runat="server" onchange="deptRoleChange();"></select>
				职位：<select id="Position" runat="server" onchange="deptRoleChange();"></select>
			</td>
		</tr>
		<tr>
			<td>
				<asp:Repeater ID="rptEmployeeList" EnableViewState="False" Runat="server">
					<HeaderTemplate>
						<span>可选人员</span><input id="chkEmpChooseLeft" type="checkbox" onclick="chooseAll(this);" /><label for="chkEmpChooseLeft">全选</label>
						<div style="height:422px;width:130px;">
							<table class="list" id="tbEmployeeList">
					</HeaderTemplate>
					<ItemTemplate>
						<tr>
							<td><input type="checkbox" value='<%# DataBinder.Eval(Container.DataItem,"Id") %>'></td>
							<td><%# DataBinder.Eval(Container.DataItem, "Name")%></td>
						</tr>
					</ItemTemplate>
					<FooterTemplate>
						</table></div>
					</FooterTemplate>
				</asp:Repeater>
			</td>
			<td>
				<input id="btnEmployeeAdd" class="btn" type="button" value="==>" onclick="addRemove();" />
			</td>
			<td>
				<asp:Repeater ID="rptChoosedEmployeeList" EnableViewState="False" Runat="server">
					<HeaderTemplate>
						<span>已选人员</span><label class="lblDel" onclick="removeAll('tbChoosedEmployeeList','tbEmployeeList');">[全删]</label>
						<div style="height:422px;width:130px;"><table class="choosedList" id="tbChoosedEmployeeList">
					</HeaderTemplate>
					<ItemTemplate>
						<tr>
							<td><input type="hidden" value='<%# DataBinder.Eval(Container.DataItem,"Id") %>'><%# DataBinder.Eval(Container.DataItem, "Name")%></td>
							<td><span class="tdDel" title="移除" onclick="remove(this,'tbEmployeeList');">&nbsp;X&nbsp;</span></td>
						</tr>
					</ItemTemplate>
					<FooterTemplate>
						</table></div>
					</FooterTemplate>
				</asp:Repeater>
			</td>
		</tr>
	</table>
	<span style="width:350px;padding-left:180px;">
		<input id="btnSubmit" class="btn" type="button" value="保存" onclick="authSubmit();"/>
		<input id="btnClose" class="btn" type="button" value="关闭" onclick="window.close();"/>
	</span>
</form>
</body>
<script language="javascript">
    //使用数组取得表格值   
    function getRowText(objId) {
        var arr = new Array();
        var count = 0;
        var tb = document.getElementById(objId);
        for (var i = 0; i < tb.rows.length; i++) {
            var row = tb.rows[i];
            arr[count] = row.cells[0].firstChild.value;
            count++;
        }
        return arr;
    }
    //授权提交
    function authSubmit() {
        if (!confirm("确认提交吗?")) {
            return false;
        }
        var arr = getRowText("tbChoosedEmployeeList");
        var empIds = arr.join(",");
        var id = getSpecProp("id");
        $.ajax({
            type: "POST",
            url: "Exam.mvc/AuthOpt",
            data: "id=" + id + "&empIds=" + empIds,
            dataType: "json",
            success: function (response) {
                if (response == "Succeed") {
                    alert("权限提交成功!");
                    window.close();
                }
                else {
                    alert(response);
                }
            }
        });
    }
    //取网址中的特定搜索字串值
    function getSpecProp(prop) {
        var searchArr = location.search.substr(1).split("&");
        for (var i = 0; i < searchArr.length; i++) {
            if (searchArr[i].split("=")[0] == prop) {
                return searchArr[i].split("=")[1];
            }
        }
        return "";
    }
    //增加/删除 授权 人员/角色
    function addRemove() {
        var tbFrom;
        var tbTo;
        var objID = event.srcElement.id;
        if (objID == "btnEmployeeAdd") {
            tbFrom = tbEmployeeList;
            tbTo = tbChoosedEmployeeList;
        }
        else if (objID == "btnEmployeeAdd2") {
            tbFrom = tbEmployeeList;
            tbTo = tbChoosedEmployeeList2;
        }
        else if (objID == "btnRoleAdd") {
            tbFrom = tbRoleList;
            tbTo = tbChoosedRoleList;
        }
        var arr = new Array();
        for (var i = 0; i < tbFrom.rows.length; ) {
            var row = tbFrom.rows[i];
            //取得CHECKBOX   
            var objCheck = row.cells[0].getElementsByTagName("INPUT")[0];
            if (objCheck != null && objCheck.checked) {
                //tbChoosedEmployeeList.appendChild(row);
                var newRow = tbTo.insertRow(0);
                var cell = newRow.insertCell(0);
                cell.innerHTML = '<input type="hidden" value="' + objCheck.value + '">' + row.cells[1].innerHTML;
                cell = newRow.insertCell(1);
                cell.innerHTML = '<span class="tdDel" title="移除" onclick="remove(this,\'' + tbFrom.id + '\');">&nbsp;X&nbsp;</span>';
                row.parentNode.removeChild(row);
            }
            else {
                i++;
            }
        }
    }
    //实现全选/全空
    function chooseAll(obj) {
        var tb;
        if (obj.id == "chkEmpChooseLeft") {
            tb = tbEmployeeList;
        }
        else if (obj.id == "chkRoleChooseLeft") {
            tb = tbRoleList;
        }
        if (obj.checked) {
            for (var i = 0; i < tb.rows.length; i++) {
                var row = tb.rows[i];
                var objCheck = row.cells[0].getElementsByTagName("INPUT")[0];
                objCheck.checked = true;
            }
        }
        else {
            for (var i = 0; i < tb.rows.length; i++) {
                var row = tb.rows[i];
                var objCheck = row.cells[0].getElementsByTagName("INPUT")[0];
                objCheck.checked = false;
            }
        }
    }
    function deptRoleChange() {
        $.ajax({
            type: "POST",
            url: "File.mvc/GetUnauthedEmp",
            data: "deptId=" + $("#Dept").val() + "&posId=" + $("#Position").val(),
            dataType: "json",
            success: function (response) {
                if (response.error != null) {
                    alert(response.error.Message);
                }
                else {
                    while (tbEmployeeList.rows.length > 0) {
                        tbEmployeeList.deleteRow();
                    }
                    var choosedEmployeeArr = getRowText("tbChoosedEmployeeList");
                    var choosedEmployeeIds = ',' + choosedEmployeeArr.join(",") + ',';
                    for (var i = 0; i < response.length; i++) {
                        var searchId = ',' + response[i].Id + ',';
                        if (choosedEmployeeIds.indexOf(searchId) < 0) {
                            var newRow = tbEmployeeList.insertRow(tbEmployeeList.rows.length);
                            var cell = newRow.insertCell(0);
                            cell.innerHTML = '<input type="checkbox" value=\'' + response[i].Id + '\'>';
                            cell = newRow.insertCell(1);
                            cell.innerHTML = response[i].Name;
                        }
                    }
                }
            }
        });
    }

    function remove(obj, tbID) {
        var tb = document.getElementById(tbID);
        var newRow = tb.insertRow(0);
        var cell = newRow.insertCell(0);
        cell.innerHTML = '<input type="checkbox" value=\'' + obj.parentElement.parentElement.cells[0].firstChild.value + '\' checked>';
        cell = newRow.insertCell(1);
        cell.innerHTML = obj.parentElement.parentElement.cells[0].innerText;
        obj.parentElement.parentElement.parentElement.removeChild(obj.parentElement.parentElement); //移除该行
    }
    function removeAll(fromTbId, toTbId) {
        var fromTb = document.getElementById(fromTbId);
        var toTb = document.getElementById(toTbId);
        while (fromTb.rows.length > 0) {
            var row = fromTb.rows[0];
            var newRow = toTb.insertRow(0);
            var cell = newRow.insertCell(0);
            cell.innerHTML = '<input type="checkbox" value=\'' + row.cells[0].firstChild.value + '\' checked>';
            cell = newRow.insertCell(1);
            cell.innerHTML = row.cells[0].innerText;
            fromTb.deleteRow(0); //移除该行
        }
    }
</script>
</HTML>